from PyQt5.QtWidgets import ( QHBoxLayout, QLabel, QPushButton,QSpinBox, QDoubleSpinBox,QGroupBox,QVBoxLayout,QCheckBox)
from components.actiondialog import ActionDialog

class MouseScrollDialog(ActionDialog):
    def __init__(self, action=None, parent=None):
        super().__init__(parent)

        self.action = action if action else {"type": "scroll", "amount": 0, "x": 0, "y": 0}
        print(action)
       #  # 滚动量
        amount_layout = QHBoxLayout()
        amount_layout.addWidget(QLabel("滚动量(正数向上，负数向下):"))
        self.amount_spin = QSpinBox()
        self.amount_spin.setRange(-1000, 1000)
        self.amount_spin.setValue(self.action.get("amount", 0))
        amount_layout.addWidget(self.amount_spin)

       #  # 坐标(可选)
        coord_group = QGroupBox("坐标(可选)")
        coord_layout = QVBoxLayout()
       #
        x_layout = QHBoxLayout()
        x_layout.addWidget(QLabel("X坐标:"))
        self.x_spin = QSpinBox()
        self.x_spin.setRange(0, 9999)
        if self.action.get("x", 0) is None:
         self.x_spin.setValue(0)
        else:
         self.x_spin.setValue(self.action.get("x", 0))
        self.x_check = QCheckBox("指定")
        self.x_check.setChecked(self.action.get("x") is not None )
        self.x_check.stateChanged.connect(self.toggle_x)
        x_layout.addWidget(self.x_spin)
        x_layout.addWidget(self.x_check)

        y_layout = QHBoxLayout()
        y_layout.addWidget(QLabel("Y坐标:"))
        self.y_spin = QSpinBox()
        self.y_spin.setRange(0, 9999)

        if self.action.get("x", 0) is None:
            self.y_spin.setValue(0)
        else:
            self.y_spin.setValue(self.action.get("xy", 0))

        self.y_check = QCheckBox("指定")
        self.y_check.setChecked(self.action.get("y") is not None)
        self.y_check.stateChanged.connect(self.toggle_y)
        y_layout.addWidget(self.y_spin)
        y_layout.addWidget(self.y_check)

        coord_layout.addLayout(x_layout)
        coord_layout.addLayout(y_layout)
        coord_group.setLayout(coord_layout)

       #  # 按钮
        button = QPushButton("确定")
        button.clicked.connect(self.accept)

        self.layout.addLayout(amount_layout)
        self.layout.addWidget(coord_group)
        self.layout.addWidget(button)

        self.toggle_x()
        self.toggle_y()

    def toggle_x(self):
        self.x_spin.setEnabled(self.x_check.isChecked())

    def toggle_y(self):
        self.y_spin.setEnabled(self.y_check.isChecked())

    def accept(self):
        x = self.x_spin.value() if self.x_check.isChecked() else None
        y = self.y_spin.value() if self.y_check.isChecked() else None

        desc = f"鼠标滚轮滚动 {self.amount_spin.value()} 单位"
        if x is not None and y is not None:
            desc += f" (在位置 {x}, {y})"
        elif x is not None:
            desc += f" (在X位置 {x})"
        elif y is not None:
            desc += f" (在Y位置 {y})"

        self.action = {
            "type": "scroll",
            "amount": self.amount_spin.value(),
            "x": x if self.x_check.isChecked() else None,  # 确保未选中时为None
            "y": y if self.y_check.isChecked() else None,  # 确保未选中时为None
            "desc": desc
        }
        super().accept()
